/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.project.web.projectformrelation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.project.entity.projectformrelation.ProjectFormRelation;
import com.idenger.modules.project.service.projectformrelation.ProjectFormRelationService;

/**
 * 项目表单关系Controller
 * @author chenlin
 * @version 2018-04-11
 */
@Controller
@RequestMapping(value = "${adminPath}/project/projectformrelation/projectFormRelation")
public class ProjectFormRelationController extends BaseController {

	@Autowired
	private ProjectFormRelationService projectFormRelationService;
	
	@ModelAttribute
	public ProjectFormRelation get(@RequestParam(required=false) String id) {
		ProjectFormRelation entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectFormRelationService.get(id);
		}
		if (entity == null){
			entity = new ProjectFormRelation();
		}
		return entity;
	}
	
	@RequiresPermissions("project:projectformrelation:projectFormRelation:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectFormRelation projectFormRelation, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectFormRelation> page = projectFormRelationService.findPage(new Page<ProjectFormRelation>(request, response), projectFormRelation); 
		model.addAttribute("page", page);
		return "modules/project/projectformrelation/projectFormRelationList";
	}

	@RequiresPermissions("project:projectformrelation:projectFormRelation:view")
	@RequestMapping(value = "form")
	public String form(ProjectFormRelation projectFormRelation, Model model) {
		model.addAttribute("projectFormRelation", projectFormRelation);
		return "modules/project/projectformrelation/projectFormRelationForm";
	}

	@RequiresPermissions("project:projectformrelation:projectFormRelation:edit")
	@RequestMapping(value = "save")
	public String save(ProjectFormRelation projectFormRelation, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, projectFormRelation)){
			return form(projectFormRelation, model);
		}
		projectFormRelationService.save(projectFormRelation);
		addMessage(redirectAttributes, "保存项目表单关系成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectformrelation/projectFormRelation/?repage";
	}
	
	@RequiresPermissions("project:projectformrelation:projectFormRelation:edit")
	@RequestMapping(value = "delete")
	public String delete(ProjectFormRelation projectFormRelation, RedirectAttributes redirectAttributes) {
		projectFormRelationService.delete(projectFormRelation);
		addMessage(redirectAttributes, "删除项目表单关系成功");
		return "redirect:"+Global.getAdminPath()+"/project/projectformrelation/projectFormRelation/?repage";
	}

}